package ipl.projet.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Controleur extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	public Controleur() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		if(session == null || session.getAttribute("login") == null) {
			getServletContext().getNamedDispatcher("connexion.html").forward(
					request, response);
			return;
		}
		String url = request.getServletPath().substring(1);
		RequestDispatcher rd = getServletContext().getNamedDispatcher(url);
		if (url.isEmpty() || rd == null) {
			response.sendRedirect(response.encodeRedirectURL("accueil.html"));
			return;
		} else {
			rd.forward(request, response);
		}
	}
}
